import 'package:flutter/material.dart';
import 'dart:math' as math;
import 'dart:async';
import 'home.dart';


class game extends StatefulWidget {
  const game({Key? key}) : super(key: key);

  @override
  State<game> createState() => _gameState();
}

class _gameState extends State<game> {
  ///游戏机制
  Timer ?timer2;///这里创建定时器
  int timezaoxing = 0;///这里设定一个造型计时器
  String  nulls ='assets/zhiwu/nulls.png';///空图
  String xuanzhongzhiwu = '';///这里是选中的是卡槽的哪个植物
  String fangzhizhiwu = '';///这个是在选好的地皮种植下去选中的植物
  List zhongzhiweizhi =[
    {'x':545.0,'y':160.0},
    {'x':610.0,'y':160.0},
    {'x':675.0,'y':160.0},
    {'x':740.0,'y':160.0},
    {'x':805.0,'y':160.0},
    {'x':870.0,'y':160.0},
    {'x':935.0,'y':160.0},
    {'x':1000.0,'y':160.0},
    {'x':1065.0,'y':160.0},
    {'x':545.0,'y':235.0},
    {'x':610.0,'y':235.0},
    {'x':675.0,'y':235.0},
    {'x':740.0,'y':235.0},
    {'x':805.0,'y':235.0},
    {'x':870.0,'y':235.0},
    {'x':935.0,'y':235.0},
    {'x':1000.0,'y':235.0},
    {'x':1065.0,'y':235.0},
    {'x':545.0,'y':310.0},
    {'x':610.0,'y':310.0},
    {'x':675.0,'y':310.0},
    {'x':740.0,'y':310.0},
    {'x':805.0,'y':310.0},
    {'x':870.0,'y':310.0},
    {'x':935.0,'y':310.0},
    {'x':1000.0,'y':310.0},
    {'x':1065.0,'y':310.0},
    {'x':545.0,'y':385.0},
    {'x':610.0,'y':385.0},
    {'x':675.0,'y':385.0},
    {'x':740.0,'y':385.0},
    {'x':805.0,'y':385.0},
    {'x':870.0,'y':385.0},
    {'x':935.0,'y':385.0},
    {'x':1000.0,'y':385.0},
    {'x':1065.0,'y':385.0},
    {'x':545.0,'y':460.0},
    {'x':610.0,'y':460.0},
    {'x':675.0,'y':460.0},
    {'x':740.0,'y':460.0},
    {'x':805.0,'y':460.0},
    {'x':870.0,'y':460.0},
    {'x':935.0,'y':460.0},
    {'x':1000.0,'y':460.0},
    {'x':1065.0,'y':460.0},
  ];///这里是所有植物能种植的位置
  List zhongzhiweizhibianhao =[];///种植位置的编号
  ///天然阳光
  int timesun = 0;///这里设定一个阳光计时器
  int sun = 0;///这是初始阳光值
  Map sunxy ={'x':0,'y':-10};///这是阳光xy
  bool sunshifouxianshi = false;///是否显示阳光
  Map sunsuijiXY ={'x':0,'y':0};
  bool sunshifoubeishouji =false;///阳光是否被收集
  bool sunshifoushengcheng = true;
  zhizaosun(){
    setState(() {
      sunsuijiXY['x'] = (380 + math.Random().nextInt(301));
      sunsuijiXY['y'] = (99 + math.Random().nextInt(200));
      sunxy['x'] = sunsuijiXY['x'];
      sunxy['y'] = -10;
      Future.delayed(Duration(milliseconds: 300),()async{
        sunshifouxianshi = true;
      });
    });
  }///创建一个阳光并赋予初始值
  ///向日葵
  List XRKdonghua =[
    'assets/zhiwu/XRK/XRK0.png',
    'assets/zhiwu/XRK/XRK1.png',
    'assets/zhiwu/XRK/XRK2.png',
    'assets/zhiwu/XRK/XRK3.png',
    'assets/zhiwu/XRK/XRK4.png',
    'assets/zhiwu/XRK/XRK5.png',
    'assets/zhiwu/XRK/XRK6.png',
    'assets/zhiwu/XRK/XRK7.png',
    'assets/zhiwu/XRK/XRK8.png',
    'assets/zhiwu/XRK/XRK9.png',
    'assets/zhiwu/XRK/XRK10.png',
    'assets/zhiwu/XRK/XRK11.png',
    'assets/zhiwu/XRK/XRK12.png',
  ];///向日葵摇摆动画
  List xiangrikuibianhao =[];///种植向日葵的编号
  String XRKzaoxin = 'assets/zhiwu/XRK/XRK0.png';///向日葵造型
  List xiangrikuixy = [];///这里创建一个列表来记录种植的向日葵xy
  ///豌豆射手
  int timeWD = 0;///豌豆发射计时器
  List WDSSdonghua =[
    'assets/zhiwu/WDSS/WDSS0.png',
    'assets/zhiwu/WDSS/WDSS1.png',
    'assets/zhiwu/WDSS/WDSS2.png',
    'assets/zhiwu/WDSS/WDSS3.png',
    'assets/zhiwu/WDSS/WDSS4.png',
    'assets/zhiwu/WDSS/WDSS5.png',
    'assets/zhiwu/WDSS/WDSS6.png',
    'assets/zhiwu/WDSS/WDSS7.png',
    'assets/zhiwu/WDSS/WDSS8.png',
    'assets/zhiwu/WDSS/WDSS9.png',
    'assets/zhiwu/WDSS/WDSS10.png',
    'assets/zhiwu/WDSS/WDSS11.png',
    'assets/zhiwu/WDSS/WDSS12.png',
  ];///豌豆射手摇摆动画
  List WDshuliang = [];///记录每个豌豆发射的豌豆
  String WDSSzaoxin = 'assets/zhiwu/WDSS/WDSS0.png';///豌豆射手造型
  List wandousheshouxy = [];///这里创建一个列表来记录种植的豌豆射手xy
  List  wandousheshoubianhao =[];///种植豌豆射手的编号
  List WDXY = [];///豌豆XY
  List WDshifoushengcheng =[];///是否允许生成豌豆
  List WDshifouxianshi = [];///是否显示豌豆
  WDzhizao(int index){
    setState(() {
    if(WDshifouxianshi[index]=='false'){
      WDXY[index]['x']=wandousheshouxy[index]['x'];
      WDXY[index]['y']=wandousheshouxy[index]['y'];
    }
    WDshifouxianshi[index] = 'true';
    });
  }///创建豌豆并赋予初始值
  ///向日葵的阳光
  List XRKsunXY =[];///向日葵生产的阳光xy
  XRKzhizaosun(int index){
    setState(() {
      XRKsunXY[index]['x']=xiangrikuixy[index]['x'];
      XRKsunXY[index]['y']=xiangrikuixy[index]['y'];
      Future.delayed(Duration(seconds: 20),()async{
        XRKsunshifouxianshi[index] = 'true';
      });
    });
  }///创建向日葵生产的阳光并赋予初始值
  List XRKsunshifouxianshi = [];///是否显示向日葵阳光
  List XRKshifoushengcheng = [];///是否允许生成向日葵阳光
  ///僵尸
  int timezombe =0;
  List Zombechushengdian =[
    {'x':1095.0,'y':160.0},
    {'x':1095.0,'y':235.0},
    {'x':1095.0,'y':310.0},
    {'x':1095.0,'y':385.0},
    {'x':1095.0,'y':460.0},
  ];
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    Future.delayed(Duration(seconds: 6),()async{
      zhizaosun();
    });
    timer2 = Timer.periodic(Duration(milliseconds: 100), (timer2) {
      setState(() {
        ///这里来记录输出的东西，找bug用
        print('豌豆射手编号$wandousheshoubianhao');
        print('天然阳光是否允许生成$sunshifoushengcheng');
        print('天然阳光是否显示$sunshifouxianshi');
        print('豌豆射手数量${WDXY.length}');
        print('豌豆是否允许生成$WDshifoushengcheng');
        print('豌豆是否显示$WDshifouxianshi');
        print('天然阳光刷新时间${(timesun / 10).toInt()}');
        ///阳光计时器++
        timesun++;
        ///豌豆发射计时器++
        timeWD++;
        ///造型计时器++
        timezaoxing++;
        ///僵尸计时器++
        timezombe++;
        ///植物动画造型切换
        XRKzaoxin = XRKdonghua[timezaoxing%XRKdonghua.length];
        WDSSzaoxin = WDSSdonghua[(timezaoxing/1.5).toInt()%XRKdonghua.length];
        ///生成每个豌豆射手的豌豆
        for(int i =0;i<wandousheshoubianhao.length;i++){
          if(WDXY.length!=0&&WDshifouxianshi[i]=='false'){
            Future.delayed(Duration(milliseconds: 500),(){
              WDshifoushengcheng[i]='true';
              WDzhizao(i);
              WDshifoushengcheng[i]='false';
            });
          }
          if(WDshifouxianshi[i]=='true'){
            if(WDXY[i]['x']>1150){
              WDshifouxianshi[i] = 'false';
            }else{
              WDXY[i]['x']+=25;
            }
          }
        }
        ///生成每个向日葵的阳光
        for(int i = 0;i<xiangrikuibianhao.length;i++){
          if(XRKsunXY.length!=0&&XRKshifoushengcheng[i]=='true'){
            XRKzhizaosun(i);
            XRKshifoushengcheng[i]='false';
          }
        }
        ///收集完天然阳光重置阳光计时器
        if(sunshifoubeishouji==true){
          timesun = 0;
        }
        ///天然阳光自然下落
        if(sunshifouxianshi == true && sunshifoubeishouji!=true){
          if(sunxy['y'] >= sunsuijiXY['y']) {
            sunxy['y'] = sunsuijiXY['y'];
          }
          else{
            sunxy['y']+=10;
          }
        }
        ///生成天然阳光
        if((timesun/10).toInt()>20&&sunshifoushengcheng==true){
          zhizaosun();
          sunshifouxianshi = false;
          sunshifoushengcheng = false;
        }
      });
    });
  }
  Widget build(BuildContext context) {
    double rpx = MediaQuery.of(context).size.width/1080;
    double ww = MediaQuery.of(context).size.width;
    double hh = MediaQuery.of(context).size.height;
    return Scaffold(
      body: Container(
        width: ww,
        height: hh,
        color: Colors.black,
        child: Stack(
          children: [
            Positioned(
              top: 25*rpx,
              left: -270*rpx,
              child: Visibility(
                // visible: tiaozhuan=='第一次游戏'?true:false,
                child:Stack(
                  children: [
                    Container(
                      alignment: Alignment.center,
                      width: 1800*rpx,
                      height: 600*rpx,
                      decoration:BoxDecoration(
                        image: DecorationImage(
                          image: AssetImage('assets/beijin/shaomo.png'),
                        ),
                      ),

                    ),
                    AnimatedPositioned(
                      left:280*rpx,
                      top: 70*rpx,
                      duration: Duration(milliseconds: 200),
                      child:Container(
                        width: 140*rpx,
                        height: 900*rpx,
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.start,
                          children: [
                            Stack(
                              children: [
                                GestureDetector(
                                  onTap: (){
                                    setState(() {
                                      xuanzhongzhiwu = '向日葵';
                                    });
                                  },
                                  child: Container(
                                    margin: EdgeInsets.only(top: 25*rpx),
                                    width: 95*rpx,
                                    height: 55*rpx,
                                    decoration: BoxDecoration(
                                        image: DecorationImage(
                                            image: AssetImage('assets/kapian/xiangrikui.png'),fit: BoxFit.fill
                                        )
                                    ),
                                  ),
                                ),
                                Visibility(
                                  visible: xuanzhongzhiwu=='向日葵'?true:false,
                                  child: Positioned(
                                    top: 25*rpx,
                                    child:  Container(
                                      width: 95*rpx,
                                      height: 55*rpx,
                                      child: Image.asset('assets/kapian/xuanzhong.png',fit: BoxFit.fill,),
                                    ),
                                  ),
                                )
                              ],
                            ),
                            Stack(
                             children: [
                               GestureDetector(
                                 onTap: (){
                                   setState(() {
                                     xuanzhongzhiwu = '豌豆射手';
                                   });
                                 },
                                 child: Container(
                                   margin: EdgeInsets.only(top: 15*rpx),
                                   width: 95*rpx,
                                   height: 55*rpx,
                                   decoration: BoxDecoration(
                                       image: DecorationImage(
                                           image: AssetImage('assets/kapian/wandousheshou.png'),fit: BoxFit.fill
                                       )
                                   ),
                                 ),
                               ),
                               Visibility(
                                 visible: xuanzhongzhiwu=='豌豆射手'?true:false,
                                 child: Positioned(
                                   top: 15*rpx,
                                   child:  Container(
                                     width: 95*rpx,
                                     height: 55*rpx,
                                     child: Image.asset('assets/kapian/xuanzhong.png',fit: BoxFit.fill,),
                                   ),
                                 ),
                               )
                             ],
                           ),
                          ],
                        ),
                      ) ,
                    ),
                    AnimatedPositioned(
                      left:280*rpx,
                      top: 30*rpx,
                      duration: Duration(milliseconds: 200),
                      child:Container(
                        width: 200*rpx,
                        height: 60*rpx,
                        child: Stack(
                          children: [
                            Positioned(
                              top: 15*rpx,
                              left: 30*rpx,
                              child: Container(
                                  padding: EdgeInsets.only(bottom: 0*rpx),
                                  width: 100*rpx,
                                  height: 30*rpx,
                                  decoration: BoxDecoration(
                                      borderRadius: BorderRadius.only(topRight: Radius.circular(12*rpx),bottomRight: Radius.circular(12*rpx)),
                                      color: Color.fromRGBO(0, 0, 0, 0.7)
                                  ),
                                  child:Row(
                                    mainAxisAlignment: MainAxisAlignment.center,
                                    children: [
                                      Text(sun.toString(),style: TextStyle(color:Colors.white),)
                                    ],
                                  )
                              ) ,
                            ),
                            Positioned(
                              top:-3*rpx,
                              left: 0*rpx,
                              child: Container(
                                height: 60*rpx,
                                child: Image.asset('assets/sun/sun.png',fit: BoxFit.cover,),
                              )
                            )
                          ],
                        ),
                      )
                    ),
                    ...List.generate(WDXY.length, (index) {
                      return Visibility(
                        visible: WDshifouxianshi[index]=='true'?true:false,
                        child: AnimatedPositioned(
                            duration: Duration(milliseconds: 500),
                            left: WDXY[index]['x']*rpx,
                            top:  WDXY[index]['y']*rpx,
                            child: GestureDetector(
                              child: Container(
                                  margin: EdgeInsets.only(top:17*rpx,left: 35*rpx),
                                  width: 15*rpx,
                                  height: 15*rpx,
                                  child: Image.asset('assets/shengchengwu/wandou.png',fit: BoxFit.cover,)
                              ),
                            )
                        ),
                      );
                    }),
                    ...List.generate(zhongzhiweizhi.length, (index){
                      return AnimatedPositioned(
                        left: zhongzhiweizhi[index]['x']*rpx,
                        top: zhongzhiweizhi[index]['y']*rpx,
                        duration: Duration(milliseconds: 100),
                        child: GestureDetector(
                           onTap: (){
                             setState(() {
                               print('我是$index');
                               if(xuanzhongzhiwu=='向日葵'&&sun>49&&!wandousheshoubianhao.contains(index)&&!xiangrikuibianhao.contains(index)){
                                 xiangrikuixy.add({'x':zhongzhiweizhi[index]['x'],'y':zhongzhiweizhi[index]['y']});
                                 zhongzhiweizhibianhao.add(index);
                                 xiangrikuibianhao.add(index);
                                 sun = sun-50;
                                 fangzhizhiwu = '向日葵';
                                 XRKsunXY.add({'x':zhongzhiweizhi[index]['x'],'y':zhongzhiweizhi[index]['y']});
                                 XRKsunshifouxianshi.add('false');
                                 XRKshifoushengcheng.add('true');
                               }
                               if(xuanzhongzhiwu=='豌豆射手'&&sun>99&&!xiangrikuibianhao.contains(index)&&!wandousheshoubianhao.contains(index)){
                                 wandousheshouxy.add({'x':zhongzhiweizhi[index]['x'],'y':zhongzhiweizhi[index]['y']});
                                 zhongzhiweizhibianhao.add(index);
                                 wandousheshoubianhao.add(index);
                                 sun = sun -100;
                                 fangzhizhiwu = '豌豆射手';
                                 WDXY.add({'x':zhongzhiweizhi[index]['x'],'y':zhongzhiweizhi[index]['y']});
                                 WDshifouxianshi.add('false');
                                 WDshifoushengcheng.add('true');
                               }
                             });
                           },
                          child: Container(
                            width: 65*rpx,
                            height: 70*rpx,
                            child: Stack(
                              children: [
                                Container(
                                  margin: EdgeInsets.only(left: 5*rpx),
                                  height: 60*rpx,
                                  width: 50*rpx,
                                  child: Image.asset(xiangrikuibianhao.contains(index)&&zhongzhiweizhibianhao.contains(index)?XRKzaoxin:nulls,),
                                ),
                                Container(
                                  margin: EdgeInsets.only(left: 5*rpx),
                                  height: 60*rpx,
                                  width: 50*rpx,
                                  child: Image.asset(wandousheshoubianhao.contains(index)&&zhongzhiweizhibianhao.contains(index)?WDSSzaoxin:nulls,),
                                ),
                              ],
                            )
                          ),
                        )
                      );
                    }),
                    Visibility(
                      visible: sunshifouxianshi,
                      child: AnimatedPositioned(
                          duration: Duration(seconds: 1),
                          left: sunxy['x'].toDouble(),
                          top: sunxy['y'].toDouble(),
                          child: GestureDetector(
                            onTap: (){
                              setState(() {
                                sunxy['x'] = 170;
                                sunxy['y'] = 20;
                                sunshifoubeishouji =true;
                                Future.delayed(Duration(seconds: 1),()async{
                                  setState(() {
                                    sun= sun+25;
                                    sunshifoushengcheng = true;
                                    sunshifouxianshi =false;
                                    Future.delayed(Duration(seconds: 1),(){
                                      sunshifoubeishouji =false;
                                    });
                                  });
                                });
                              });
                            },
                            child: Container(
                              height: 45*rpx,
                              child: Image.asset('assets/sun/sun.png',fit: BoxFit.cover,)
                            ),
                          )
                      ),
                    ),
                    ...List.generate(XRKsunXY.length, (index) {
                      return Visibility(
                        visible: XRKsunshifouxianshi[index]=='true'?true:false,
                        child: AnimatedPositioned(
                            duration: Duration(seconds: 1),
                            left: XRKsunXY[index]['x']*rpx,
                            top:  XRKsunXY[index]['y']*rpx,
                            child: GestureDetector(
                              onTap: (){
                                setState(() {
                                  XRKsunXY[index]['x'] = 275;
                                  XRKsunXY[index]['y'] = 30;
                                  Future.delayed(Duration(seconds: 1),()async{
                                    setState(() {
                                      sun= sun+25;
                                      XRKsunshifouxianshi[index] = 'false';
                                      XRKshifoushengcheng[index]= 'true';
                                    });
                                  });
                                });
                              },
                              child: Container(
                                margin: EdgeInsets.only(left: 5*rpx),
                                  height: 45*rpx,
                                  child: Image.asset('assets/sun/sun.png',fit: BoxFit.cover,)
                              ),
                            )
                        ),
                      );
                    }),
                  ],
                )
              ),
            )
          ],
        ),
      )
    );
  }
}
